<?php 
Class goodsController extends appController
{
	
	public function ordering()
	{
		$id=isset($_POST['id'])?intval($_POST['id']):'';
		$val=isset($_POST['val'])?intval($_POST['val']):'';
		$result=Model('pro')->ordering($val,$id);
		print_r(json_encode($result));
	}
	
	/**
	 * 删除品牌
	 */
	public function del_brand()
	{
		$this->admin_priv('del_brand', 2);
		$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
		$result = array('error' => 1, 'content' => '删除失败');
		$goods = Model('goods');
		$res = $goods->del(array('table' => 'goods_brand', 'id' => $id));
		if($res)
		{
			$result['error'] = 0;
			$result['content'] = '删除成功';
		}
		print_r(json_encode($result));
	}
	
	public function brand_order()
	{
		$result=array('error'=>1,'content'=>'');
		$id=isset($_POST['id'])?intval($_POST['id']):'';
		$info['ordering']=isset($_POST['val'])?intval($_POST['val']):'';
		$res=Model('goods')->update($info,array('table'=>'goods_brand','id'=>$id));
		if($res)
		{
			$result['error']=0;
			$result['content']=Lang('SUCCESS_EDIT');
		}
		else
		{
			$result['content']=Lang('INVALID_OPERATION');
		}
		
		print_r(json_encode($result));
	}
	
	/**
	 * 显示品牌列表
	 */
	public function brand_list()
	{
		$page = yf_empty(intval($_REQUEST['page']), 1, true);
		$sort['keyword'] = yf_empty(trim($_GET['k']), '', true);
		$goods_list = Model('goods')->get_brand_list($sort, $page);
		$this->assign('art_list',$goods_list['result']);
		$this->assign('art_page',$goods_list['page']);
		$this->display('brand_list.html');
	}
	
	/**
	 * 显示添加品牌页面
	 */
	public function add_brand()
	{
		$this->admin_priv('add_brand', 2);
		$this->assign('action','new_brand');
		$this->display('brand.html');
	}
	
	/**
	 * 添加品牌数据
	 */
	public function new_brand()
	{
		$this->admin_priv('add_brand', 2);
		$result = array('error' => 1, 'content' => '');
		
		$en_name = yf_empty($_POST['en_name'], '', true);
		$info['brand_name'] = CH_SC.yf_empty($_POST['brand_name'], "品牌名称不能为空").DIG_EC.EN_SC.$en_name.DIG_EC;
		$info['first_letter'] = yf_empty($_POST['first_letter'], '#', true);
		
		if($info['first_letter'] == '#')
		{
			$result['content'] = "请选择开头字母";
			$this->showMsg($result);
		}
		
		$info['brand_logo'] = yf_empty($_POST['brand_logo'], '', true);
		$info['brand_desc'] = yf_empty($_POST['brand_desc'], '', true);
		$info['site_url'] = yf_empty($_POST['site_url'], '', true);
		$info['is_show'] = yf_empty(intval($_POST['is_show']), 1, true);
		$goods = Model('goods');
		$res = $goods->insert($info, 'goods_brand');
		if($res)
		{
			$result['error'] = 0;
			$result['content'] = Lang('SUCCESS_ADD');
		}
		else
		{
			$result['content'] = Lang('INVALID_OPERATION');
		}
		$this->showMsg($result);
	}
	
	/**
	 * 显示品牌编辑页面
	 */
	public function edit_brand()
	{
		$this->admin_priv('edit_brand', 2);
		$id = yf_empty(intval($_GET['id']), '', true);
		$art = Model('goods')->get_brand($id);
		$this->assign('id',$id);
		$this->assign('art',$art);
	
		$this->assign('action','update_brand');
		$this->display('brand.html');
	}
	
	/**
	 * 更新品牌信息
	 */
	public function update_brand()
	{
		$this->admin_priv('edit_brand', 2);
		$result = array('error' => 1, 'content' => '');
		$id = yf_empty(intval($_POST['id']), '', true);
		$en_name = yf_empty($_POST['en_name'], '', true);
		$info['brand_name'] = CH_SC.yf_empty($_POST['brand_name'], "品牌的名称不能为空").DIG_EC.EN_SC.$en_name.DIG_EC;
		$info['first_letter'] = yf_empty($_POST['first_letter'], '#', true);
		
		if($info['first_letter'] == '#')
		{
			$result['content'] = "请选择开头字母";
			$this->showMsg($result);
		}
		
		$info['brand_logo'] = yf_empty($_POST['brand_logo'], '', true);
		$info['brand_desc'] = yf_empty($_POST['brand_desc'], '', true);
		$info['site_url'] = yf_empty($_POST['site_url'], '', true);
		$info['is_show'] = yf_empty(intval($_POST['is_show']), 1, true);
		$goods = Model('goods');
		$res = $goods->update($info, array('table' => 'goods_brand', 'id' => $id));
		if($res)
		{
			$result['error'] = 0;
			$result['content'] = Lang('SUCCESS_EDIT');
		}
		else
		{
			$result['content'] = Lang('INVALID_OPERATION');
		}
		$this->showMsg($result);
	}
	
	/**
	 * 显示编辑页面
	 */
	public function edit_attr()
	{
		$this->admin_priv('edit_attr', 2);
		$id = intval($_GET[id]);
		$art = Model('goods')->get_attr($id);
		$this->assign('id', $id);
		$this->assign('art', $art);
		$this->assign('action', 'update_attr');
		$this->display('attr.html');
	}
	
	public function update_attr()
	{
		$this->admin_priv('edit_attr', 2);
		$result = array('error' => 1,'content' => '');
	    $id = intval($_POST['id']);
		$_POST['attr_name'] = isset($_POST['attr_name']) ? hy_substr($_POST['attr_name'], 7) : '';
		$en_name = $_POST['en_attr_name'];
		$info['name'] = CH_SC.$_POST['attr_name'].DIG_EC.EN_SC.$en_name.DIG_EC;
		//只要非空的属性项
		if(is_array($_POST['name']))
		{
			foreach($_POST['name'] as $k => $v)
			{
				if($v === '')
				{
					unset($_POST['name'][$k]);
					unset($_POST['en_name'][$k]);
					unset($_POST['thumb'][$k]);
					unset($_POST['color'][$k]);
				}
			}
			$en_name = implode(',', $_POST['en_name']);
			$ch_name = implode(',', $_POST['name']);
			$info['value'] = CH_SC.$ch_name.DIG_EC.EN_SC.$en_name.DIG_EC;
			$info['thumb'] = implode(',', $_POST['thumb']);
			$info['color'] = implode(',', $_POST['color']);
		}
		else
		{
			if(!empty($_POST['name']))
			{
				$en_name = isset($_POST['en_name']) ? $_POST['en_name'] : '';
				$info['value'] = isset($_POST['name']) ? CH_SC.$_POST['name'].DIG_EC.EN_SC.$en_name.DIG_EC : '';
				$info['thumb'] = isset($_POST['thumb']) ? $_POST['thumb'] : '';
				$info['color'] = isset($_POST['color']) ? $_POST['color'] : '';
			}
		}
		$good = Model('goods');
		if(!$info['name'])
		{
			$result['content'] = '属性名不能为空';
			$this->showMsg($result);
		}
		if($_POST['old_name'] != $_POST['attr_name'])
		{
			$tags = $good->db_count(array('table' => 'goods_attr', 'name' => $_POST['attr_name']));
		}
		else 
		{
			$tags = 0;
		}
		if($tags == 0)
		{
			$res = $good->update($info, array('table' => 'goods_attr', 'id' => $id));
			if($res)
			{
				$result['error'] = 0;
				$result['content'] = Lang('SUCCESS_EDIT');
			}
			else
			{
				$result['content'] = Lang('INVALID_OPERATION');
			}
		}
		else
		{
			$result['content'] = '属性名已存在';
			$this->showMsg($result);
		}
		$this->showMsg($result);
	}
	
	/**
	 * 显示商品属性添加页面
	 */
	public function add_attr()
	{
		$this->admin_priv('add_attr', 2);
		$this->assign('action', 'new_attr');
		$this->display('attr.html');
	}
	
	/**
	 * 添加商品属性
	 */
	public function new_attr()
	{
		$this->admin_priv('add_attr', 2);
		$result = array('error' => 1, 'content' => '');
		
		$_POST['attr_name'] = hy_substr(yf_empty($_POST['attr_name'], "请输入属性名称"), 7);
		$en_name = $_POST['en_attr_name'];
		$info['name'] = CH_SC.$_POST['attr_name'].DIG_EC.EN_SC.$en_name.DIG_EC;
		//只要非空的属性项
		if(is_array($_POST['name']))
		{
			foreach($_POST['name'] as $k => $v)
			{
				if($v === '')
				{
					unset($_POST['name'][$k]);
					unset($_POST['en_name'][$k]);
					unset($_POST['thumb'][$k]);
					unset($_POST['color'][$k]);
				}
			}
			$en_name = implode(',', $_POST['en_name']);
			$ch_name = implode(',', $_POST['name']);
			$info['value'] = CH_SC.$ch_name.DIG_EC.EN_SC.$en_name.DIG_EC;
			$info['thumb'] = implode(',', $_POST['thumb']);
			$info['color'] = implode(',', $_POST['color']);
		}
		else 
		{
			if(!empty($_POST['name']))
			{
				$en_name = isset($_POST['en_name']) ? $_POST['en_name'] : '';
				$info['value'] = isset($_POST['name']) ? CH_SC.$_POST['name'].DIG_EC.EN_SC.$en_name.DIG_EC : '';
				$info['thumb'] = isset($_POST['thumb']) ? $_POST['thumb'] : '';
				$info['color'] = isset($_POST['color']) ? $_POST['color'] : '';
			}
		}
		$good = Model('goods');
		
		$tags = $good->db_count(array('table' => 'goods_attr', 'name' => $info['name']));
		if($tags==0)
		{
			$res = $good->insert($info,'goods_attr');
			if($res)
			{
				$result['error'] = 0;
				$result['content'] = Lang('SUCCESS_ADD');
			}
			else
			{
				$result['content'] = Lang('INVALID_OPERATION');
			}
		}
		else 
		{
			$result['content']='属性名已存在';
			$this->showMsg($result);
		}
		
		$this->showMsg($result);
	}
	
	/**
	 * 显示商品属性列表
	 */
	public function attr_list()
	{
		$page = yf_empty(intval($_REQUEST['page']), 1, true);
		$art_list = Model('goods')->get_attr_list($page);
		$this->assign('art_list',$art_list['result']);
		$this->assign('art_page',$art_list['page']);
		$this->display('attr_list.html');
	}
	
	/**
	 * 删除商品属性
	 */
	public function del_attr()
	{
		$this->admin_priv('del_attr', 2);
		$res = array('error' => 1, 'content' => '删除失败');
		$id = yf_empty(intval($_REQUEST['id']), "System Error");
		$check = Model('goods')->del_attr($id);
		if($check)
		{
			$res = array('error' => 0, 'content' => '删除成功');
		}
		$this->showMsg($res);
	}
	
	public function selectattr()
	{
		$result=array('error'=>1,'content'=>'获取数据失败','data'=>'');
		$id=intval($_POST['id']);
		$attr_list=Model('goods')->get_all_attr();
		$this->assign('attr_list',$attr_list);
		$tpl='<div class="am-g am-margin-top"><div class="am-u-sm-2 am-text-right">%s</div><div class="am-u-sm-4 am-u-end">%s</div></div>';

		$this->display('selectattr.html');
		//print_r(json_encode($result));
	}
	public function selecttype()
	{
		$result=array('error'=>1,'content'=>'获取数据失败','data'=>'');
		$id=intval($_POST['id']);
		$attr=Model('goods')->get_goods_spec($id);
		$html='';
		$tpl='<div class="am-g am-margin-top"><div class="am-u-sm-2 am-text-right">%s</div><div class="am-u-sm-4 am-u-end">%s</div></div>';
		if(!empty($attr))
		{
			foreach($attr as $k=>$v)
			{
				if($v['type']==1)
				{
					$input='<div class="am-btn-group" data-am-button>';
					foreach($v['field_list'] as $kk=>$vv)
					{
						$input.='<label class="am-btn am-btn-default am-btn-xs"><input type="radio" name="val_spec_'.$v['id'].'" value="'.$vv['val'].'"  >'.$vv['name'].'</label>';
					}
					$input.='</div>';
				}
				elseif($v['type']==2)
				{
					$ch_txt=hy_dig_filter($v['field'],1);
					$en_txt=hy_dig_filter($v['field'],2);
					$input='<input type="text" value="'.$ch_txt.'" name="val_spec_'.$v['id'].'" />';
					if(DIGLOSSIA)
					{
					$input.='<br>';
					$input.='<input type="text" value="'.$en_txt.'" name="val_spec_'.$v['id'].'" />';
					}
				}
				elseif($v['type']==3)
				{
					$input='<div class="am-btn-group" data-am-button>';
					foreach($v['field_list'] as $kk=>$vv)
					{
						$input.='<label class="am-btn am-btn-default am-btn-xs"><input type="checkbox" name="val_spec_'.$v['id'].'" value="'.$vv['val'].'"  >'.$vv['name'].'</label>';
					}
					$input.='</div>';
				}
				$html.=sprintf($tpl,'<input type="hidden" value="'.$v['name'].'" name="spec_'.$v['id'].'" />'.$v['name'],$input);
			}
			$result['error']=0;
			$result['content']='ok';
			$result['data']=$html;
		}
	     print_r(json_encode($result));
	}
	
	/**
	 * 显示商品类型列表
	 */
	public function type_list()
	{
		$page = yf_empty(intval($_REQUEST['page']), 1, true);
		$art_list = Model('goods')->get_spec_list($page);
		$this->assign('art_list', $art_list['result']);
		$this->assign('art_page', $art_list['page']);
		$this->display('type_list.html');
	}
	public function goodslist()
	{
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$sort['cat_id']=isset($_GET['c'])?trim($_GET['c']):'';
		$cat_sort['type']=array(4);
		$this->assign('cat_list',Model('menu')->get_cat_list($cat_sort));
		$goods_list=Model('goods')->get_goods_list($page,$sort);
		$this->assign('goods_list',$goods_list['result']);
		$this->assign('art_page',$goods_list['page']);
		$this->display('goodslist.html');
	}
	public function searchgoods()
	{
		$result=array('error'=>1,'content'=>'无搜索结果','data'=>'');
		$sort['cat_id']=isset($_POST['cat_id'])?intval($_POST['cat_id']):'';
		$sort['brand_id']=isset($_POST['brand_id'])?intval($_POST['brand_id']):'';
		$sort['keyword']=isset($_POST['keyword'])?trim($_POST['keyword']):'';
		$goods_list=Model('goods')->get_goods_list(0,$sort);
		$tpl='<tr><td>%s</td><td>%s</td><td>%s</td><td><div class="am-btn-group" data-am-button=""><label class="am-btn am-btn-default am-btn-xs"><input type="checkbox" name="link_goods"  value="%d" id="option1"> 关联</label></div></td></tr>';
		if($goods_list['result'])
		{
			foreach($goods_list['result'] as $k=>$v)
			{
				$html.=sprintf($tpl,$v['goods_name'],$v['shop_price'],$v['goods_num'],$v['id']);
			}
			$result['error']=0;
			$result['data']=$html;
		}
		
		$this->showMsg($result);
		
	}
	public function del_goods_attr()
	{
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		$result=array('error'=>1,'content'=>'删除失败');
		$goods=Model('goods');
		$res=$goods->del(array('table'=>'goods_attr_extend','id'=>$id));
		if($res)
		{
			$result['error']=0;
			$result['content']='删除成功';
		}
		print_r(json_encode($result));
	
	}
	
	/**
	 * 删除商品
	 */
	public function del_good()
	{
		$this->admin_priv('del_goods', 2);
		$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
		$result = array('error' => 1, 'content' => '删除失败');
		$goods = Model('goods');
		$res = $goods->del(array('table' => 'goods', 'id' => $id));
		$res2 = $goods->del(array('table' => 'goods_gallery', 'goods_id' => $id));
		$res3 = $goods->del(array('table' => 'goods_spec_extend', 'goods_id' => $id));
		if($res && $res2 && $res3)
		{
			$result['error'] = 0;
			$result['content'] = '删除成功';
		}
		$this->showMsg($result);
	}
	
	/**
	 *显示商品编辑页 
	 */
	public function edit_good()
	{
		$this->admin_priv('edit_goods', 2);
		$id = intval($_GET['id']);
		$goods = Model('goods');
		$bands_list = Model('goods')->get_all_brand();
		$this->assign('brands_list', $bands_list);
		$type_list = $goods->get_all_spec();
		$this->assign('type_list', $type_list);
		$cat_sort['type'] = array(4);
		$this->assign('cat_list', Model('menu')->get_cat_list($cat_sort));
		$info = $goods->get_goods($id);
		$this->assign('art', $info);
		$this->assign('id', $id);
		$this->assign('action', 'update_goods');
		$this->display('goods.html');
	}
	
	/**
	 * 更新商品信息
	 */
	public function update_goods()
	{
		$this->admin_priv('edit_goods', 2);
		$result = array('error' => 1, 'content' => '');
		$id = intval($_POST['id']);
		$en_name = isset($_POST['en_name']) ? $_POST['en_name'] : '';
		$info['goods_name'] = isset($_POST['goods_name']) ? CH_SC.$_POST['goods_name'].DIG_EC.EN_SC.$en_name.DIG_EC : '';
		$en_brief = isset($_POST['en_brief']) ? $_POST['en_brief'] : '';
		$info['goods_brief'] = isset($_POST['goods_brief']) ? CH_SC.$_POST['goods_brief'].DIG_EC.EN_SC.$en_brief.DIG_EC : '';
		$en_desc = isset($_POST['en_desc']) ? $_POST['en_desc'] : '';
		$info['goods_desc'] = isset($_POST['goods_desc']) ? CH_SC.$_POST['goods_desc'].DIG_EC.EN_SC.$en_desc.DIG_EC : '';
		$info['market_price'] = hy_check($_POST['market_price'], 'f');
		$info['shop_price'] = hy_check($_POST['shop_price'], 'f');
		$info['goods_num'] = hy_check($_POST['goods_num'], 'n');
		$info['goods_sn'] = empty($_POST['goods_sn']) ? '' : $_POST['goods_sn'];
		$info['goods_type'] = intval($_POST['goods_type']);
		$info['recommend_type'] = intval($_POST['recommend_type']);
		$info['goods_status'] = intval($_POST['status']);
		$info['goods_thumb'] = $_POST['goods_thumb'];
		$info['page_title'] = $_POST['page_title'];
		$info['keywords'] = $_POST['keywords'];
		$info['abstract'] = $_POST['abstract'];
		$info['updatetime'] = $_SERVER['REQUEST_TIME'];
		$info['weixin_chain'] = $_POST['weixin_chain'];
		$info['external_chain'] = $_POST['external_chain'];
		$info['brand_id'] = intval($_POST['brand_id']);
		$info['cat_id'] = intval($_POST['cat_id']);
		$photo = is_array($_POST['photo']) ? $_POST['photo'] : array($_POST['photo']);
		$link = is_array($_POST['link_goods']) ? $_POST['link_goods'] : array($_POST['link_goods']);
		$goods_spec_id = is_array($_POST['goods_spec_id']) ? $_POST['goods_spec_id'] : array($_POST['goods_spec_id']);
		$update_links = is_array($_POST['update_links']) ? $_POST['update_links'] : array($_POST['update_links']);
		$goods_attr_id = is_array($_POST['goods_attr_id']) ? $_POST['goods_attr_id'] : array($_POST['goods_attr_id']);
		$attr_goods_img = is_array($_POST['attr_goods_img']) ? $_POST['attr_goods_img'] : array($_POST['attr_goods_img']);
		
		$attr_pid = is_array($_POST['attr_pid']) ? $_POST['attr_pid'] : array($_POST['attr_pid']);
		$attr_val = is_array($_POST['attr_name']) ? $_POST['attr_name'] : array($_POST['attr_name']);
		$attr_img = is_array($_POST['attr_img']) ? $_POST['attr_img'] : array($_POST['attr_img']);
		
		$goods = Model('goods');
		
		foreach($_POST as $k => $v)
		{
			if(preg_match('/^(val_spec_)/', $k))
			{
				$spec[$k] = $v;
			}
			elseif (preg_match('/^spec_/', $k))
			{
				$specname[$k] = $v;
			}
		}
		$spec = $this->format_spec($specname, $spec);
		$res = $goods->update($info, array('table'=>'goods', 'id'=>$id));
		
		foreach($spec as $k => $v)
		{
			unset($spec[$k]['spec_id']);
		}
			//插入相册
		if($photo[0] != '')
		{
			$photo_arr = array();
			foreach($photo as $k => $v)
			{
				$photo_arr[$k]['goods_id'] = $id;
				$photo_arr[$k]['original_pic'] = $v;
			}
			$goods->insert($photo_arr, 'goods_gallery');
		}
		//保存属性
		if($attr_pid[0] != '')
		{
			$attr_info = array();
			foreach($attr_pid as $k => $v)
			{
				$attr_info[$k]['goods_id'] = $id;
				$attr_info[$k]['attr_value'] = $attr_val[$k];
				$attr_info[$k]['goods_img'] = $attr_img[$k];
				$attr_info[$k]['attr_id'] = $attr_pid[$k];
			}
			$goods->insert($attr_info, 'goods_attr_extend');
		}
			//更新属性
			
		if($goods_attr_id[0] != '')
		{
			foreach($attr_goods_img as $k => $v)
			{
				$update_attr[$k]['goods_img'] = $v;
			}
			$goods->update($update_attr, array('table' => 'goods_attr_extend', 'id' => $goods_attr_id));
		}
		//保存和插入规格	
		if($goods_spec_id)
		{
			$goods->update($spec, array('table' => 'goods_spec_extend', 'id' => $goods_spec_id));
		}
		else 
		{
			$goods->del(array('table' => 'goods_spec_extend', 'goods_id' => $id));
			$goods->insert($spec, 'goods_spec_extend');
		}
		
	   	if(current($link) != '')
	   	{
			$count = $goods->db_count(array('table' => 'link_goods', 'goods_id' => $id, 'link_goods' => $link));
			if($count>0)
			{
				$result['content'] = '重复关联商品了';
				print_r(json_encode($result));
				exit();
			}
	   	}
			//关联商品
		$goods->update_links($id, $update_links);
			
		$link_goods = array();
		if(current($link) != '')
		{
			foreach($link as $k => $v)
			{
				$link_goods[$k]['goods_id'] = $id;
				$link_goods[$k]['link_goods'] = $v;
			}
			$goods->insert($link_goods, 'link_goods');
		}
		$result['error'] = 0;
		$result['content'] = Lang('SUCCESS_EDIT');
		$this->showMsg($result);
	}
	
	/**
	 * 显示添加商品页面
	 */
	public function add_goods()
	{
		$this->admin_priv('add_goods', 2);
	    $bands_list = Model('goods')->get_all_brand();
	    $this->assign('brands_list', $bands_list);
		$type_list = Model('goods')->get_all_spec();
		$this->assign('type_list', $type_list);
		$cat_sort['type'] = array(4);
		$this->assign('cat_list', Model('menu')->get_cat_list($cat_sort));
		$this->assign('action', 'new_goods');
		$this->display('goods.html');
	}
	
	/**
	 * 添加商品
	 */
	public function new_goods()
	{
		$this->admin_priv('add_goods', 2);
		$result = array('error' => 1, 'content' => '');
		
		$en_name = isset($_POST['en_name']) ? $_POST['en_name'] : '';
		$info['goods_name'] = isset($_POST['goods_name']) ? CH_SC.$_POST['goods_name'].DIG_EC.EN_SC.$en_name.DIG_EC : '';
		$en_brief = isset($_POST['en_brief'])?$_POST['en_brief'] : '';
		$info['goods_brief'] = isset($_POST['goods_brief']) ? CH_SC.$_POST['goods_brief'].DIG_EC.EN_SC.$en_brief.DIG_EC : '';
		$en_desc = isset($_POST['en_desc']) ? $_POST['en_desc'] : '';
		$info['goods_desc'] = isset($_POST['goods_desc']) ? CH_SC.$_POST['goods_desc'].DIG_EC.EN_SC.$en_desc.DIG_EC : '';
		$info['market_price'] = hy_check($_POST['market_price'], 'f');
		$info['shop_price'] = hy_check($_POST['shop_price'], 'f');
		$info['goods_num'] = hy_check($_POST['goods_num'], 'n');
		$info['goods_sn'] = empty($_POST['goods_sn']) ? '' : $_POST['goods_sn'];
		$info['goods_type'] = intval($_POST['goods_type']);
		$info['recommend_type'] = intval($_POST['recommend_type']);
		$info['goods_status'] = intval($_POST['status']);
		$info['goods_thumb'] = $_POST['goods_thumb'];
		$info['page_title'] = $_POST['page_title'];
		$info['keywords'] = $_POST['keywords'];
		$info['abstract'] = $_POST['abstract'];
		$info['weixin_chain'] = $_POST['weixin_chain'];
		$info['external_chain'] = $_POST['external_chain'];
		$info['brand_id'] = intval($_POST['brand_id']);
		$info['cat_id'] = intval($_POST['cat_id']);
		$info['createtime'] = $_SERVER['REQUEST_TIME'];
		$photo = is_array($_POST['photo']) ? $_POST['photo'] : array($_POST['photo']);
		$link = is_array($_POST['link_goods']) ? $_POST['link_goods'] : array($_POST['link_goods']);
		
		$attr_pid = is_array($_POST['attr_pid']) ? $_POST['attr_pid'] : array($_POST['attr_pid']);
		$attr_val = is_array($_POST['attr_name']) ? $_POST['attr_name'] : array($_POST['attr_name']);
		$attr_img = is_array($_POST['attr_img']) ? $_POST['attr_img'] : array($_POST['attr_img']);
		
		$goods = Model('goods');
		foreach($_POST as $k => $v)
		{
			if(preg_match('/^(val_spec_)/', $k))
			{
				$spec[$k] = $v;
				
			}
			elseif (preg_match('/^spec_/', $k))
			{
				$specname[$k] = $v;
			}
		}
		$spec = $this->format_spec($specname, $spec);
		$goods_id = $goods->insert($info, 'goods');
		if($goods_id)
		{
			if(($spec[0]) != '')
			{
			//插入规格
				foreach($spec as $k => $v)
				{
					$spec[$k]['goods_id'] = $goods_id;
				}
				$goods->insert($spec, 'goods_spec_extend');
			}
			//插入相册
			if($photo[0] != '')
			{
				$photo_arr = array();
				foreach($photo as $k => $v)
				{
					$photo_arr[$k]['goods_id'] = $goods_id;
					$photo_arr[$k]['original_pic'] = $v;
				}
				$goods->insert($photo_arr, 'goods_gallery');
			}
			//保存属性
			if($attr_pid[0] != '')
			{
				$attr_info = array();
				foreach($attr_pid as $k => $v)
				{
					$attr_info[$k]['goods_id'] = $goods_id;
					$attr_info[$k]['attr_value'] = $attr_val[$k];
					$attr_info[$k]['goods_img'] = $attr_img[$k];
					$attr_info[$k]['attr_id'] = $attr_pid[$k];
				}
				$goods->insert($attr_info, 'goods_attr_extend');
			}
			//关联商品
			if($link[0] != '')
			{
			   	$link_goods = array();
			   	foreach($link as $k => $v)
			   	{
					$link_goods[$k]['goods_id'] = $goods_id;
					$link_goods[$k]['link_goods'] = $v;
			    }
			    $goods->insert($link_goods, 'link_goods');
			}
			$result['error'] = 0;
			$result['content'] = Lang('SUCCESS_ADD');
		}
		else
		{
			$result['content'] = Lang('INVALID_OPERATION');
		}
		$this->showMsg($result);
	}
	
	public function del_gallery()
	{
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		$result=array('error'=>1,'content'=>'删除失败');
	
		$res=Model('goods')->del(array('table'=>'goods_gallery','id'=>$id));
		if($res)
		{
			$result['error']=0;
			$result['content']='删除成功';
		}
		print_r(json_encode($result));
	}
	
	/**
	 * 显示添加商品类型页面
	 */
	public function add_type()
	{
		$this->admin_priv('add_type', 2);
		$this->assign('action', 'new_type');
		$this->display('goods_type.html');
	}
	
	/**
	 * 删除商品类型
	 */
	public function del_type()
	{
		$this->admin_priv('del_type', 2);
		$result = array('error' => 1, 'content' => '删除失败');
		$id = $_POST['id'];
		$res = Model('goods')->del(array('table' => 'goods_type', 'id' => $id));
		if($res)
		{
			$result['error'] = 0;
			$result['content'] = '删除成功';
		}
		$this->showMsg($result);
	}
	
	/**
	 * 删除规格
	 */
	public function del_spec()
	{
		$result = array('error' => 1, 'content' => '删除失败');
		$id = $_POST['id'];
		$res = Model('goods')->del(array('table' => 'goods_spec', 'id' => $id));
		if($res)
		{
			$result['error'] = 0;
			$result['content'] = '删除成功';
		}
		$this->showMsg($result);
	}
	
	/**
	 * 添加商品类型
	 */
	public function new_type()
	{
		$this->admin_priv('add_type', 2);
		$result = array('error' => 1, 'content' => '');
		$type_name = isset($_POST['type_name']) ? hy_substr($_POST['type_name'], 7) : '';
		if(is_array($_POST['name']))
		{
			foreach($_POST['name'] as $k => $v)
			{
				if($v != '')
				{
					$en_name = isset($_POST['en_name']) ? $_POST['en_name'][$k] : '';
					$info[$k]['name'] = isset($_POST['name']) ? CH_SC.$v.DIG_EC.EN_SC.$en_name.DIG_EC : '';
					$info[$k]['type'] = isset($_POST['type']) ? $_POST['type'][$k] : 1;
					$en_field = isset($_POST['en_field']) ? $_POST['en_field'][$k] : '';
					$info[$k]['field'] = isset($_POST['field'][$k]) ? CH_SC.$_POST['field'][$k].DIG_EC.EN_SC.$en_field.DIG_EC : '';
				}
			}
		}
		else 
		{
			if(!empty($_POST['name']))
			{
				$en_name = isset($_POST['en_name']) ? $_POST['en_name'] : '';
				$info['name'] = isset($_POST['name']) ? CH_SC.$_POST['name'].DIG_EC.EN_SC.$en_name.DIG_EC : '';
				$info['type'] = isset($_POST['type']) ? $_POST['type'] : 1;
				$en_field = isset($_POST['en_field']) ? $_POST['en_field'] : '';
				$info['field'] = isset($_POST['field']) ? CH_SC.$_POST['field'].DIG_EC.EN_SC.$en_field.DIG_EC : '';
			}
		}
		$good = Model('goods');
		if(!$type_name)
		{
			$result['content'] = '类型不能为空';
			$this->showMsg($result);
		}
		
		$tags = $good->save_tags($type_name);
		if($tags)
		{
			if(is_array($_POST['name']))
			{
				foreach($_POST['name'] as $k => $v)
		       	{
		     	  	if($v !='')
			      	{
						$info[$k]['type_id'] = $tags;
			      	}
				}
			}
			else 
			{
				$info['type_id'] = $tags;
			}
		}
		else 
		{
			$result['content'] = '标签已存在';
			$this->showMsg($result);
		}
		$res = $good->insert($info, 'goods_spec');
		if($res)
		{
			$result['error'] = 0;
			$result['content'] = Lang('SUCCESS_ADD');
		}
		else
		{
			$result['content'] = Lang('INVALID_OPERATION');
		}
		$this->showMsg($result);
	}
	
	public function edit_type()
	{
		$this->admin_priv('edit_type', 2);
		$id=intval($_GET[id]);
		$art=Model('goods')->get_spec($id);
		$this->assign('art_list',$art);
		$this->assign('id',$id);
		$this->assign('action','update_type');
		$this->display('goods_type.html');
	}
	public function update_type()
	{
		$this->admin_priv('edit_type', 2);
	    $result=array('error'=>1,'content'=>'');
		$id=intval($_POST['type_id']);
		$type_name=isset($_POST['type_name'])?hy_substr($_POST['type_name'], 7):'';
		$insert_arr=array();
		$update_arr=array();
		//print_r($_POST);
		if(is_array($_POST['name']))
		{
			$attr_name=$_POST['name'];
		}
		else 
		{
			$attr_name[]=$_POST['name'];
		}
			foreach($attr_name as $k=>$v)
			{
				if($v!='')
				{
					$en_name=isset($_POST['en_name'])?$_POST['en_name'][$k]:'';
					$en_field=isset($_POST['en_field'])?$_POST['en_field'][$k]:'';
					if($_POST['attr_id'][$k]<0)
					{
						$insert_arr[$k]['type_id']=$id;
					$insert_arr[$k]['name']=isset($_POST['name'])?CH_SC.$v.DIG_EC.EN_SC.$en_name.DIG_EC:'';
					$insert_arr[$k]['type']=isset($_POST['type'])?$_POST['type'][$k]:1;
					$insert_arr[$k]['field']=isset($_POST['field'][$k])?CH_SC.$_POST['field'][$k].DIG_EC.EN_SC.$en_field.DIG_EC:'';
					}
					else 
					{
						$update_arr[$k]['id']=$_POST['attr_id'][$k];
						$update_arr[$k]['name']=isset($_POST['name'])?CH_SC.$v.DIG_EC.EN_SC.$en_name.DIG_EC:'';
						$update_arr[$k]['type']=isset($_POST['type'])?$_POST['type'][$k]:1;
						$update_arr[$k]['field']=isset($_POST['field'][$k])?CH_SC.$_POST['field'][$k].DIG_EC.EN_SC.$en_field.DIG_EC:'';
					}
				}
			}
		$good=Model('goods');
		if(!$type_name)
		{
			$result['content']='标签不能为空';
			$this->showMsg($result);
		}
		
			$tags=$good->update(array('type_name'=>$type_name),array('table'=>'goods_type','id'=>$id));
			if($tags)
			{
				$res=true;
				if(!empty($insert_arr))
				{
					$res=$good->insert($insert_arr,'goods_spec');
				}
				
				if(!empty($update_arr))
				{
					$update_arr=array_values($update_arr);//初始化键值
					$id_arr=array();
					foreach($update_arr as $k=>$v)
					{
						$id_arr[]=$v['id'];
						unset($update_arr[$k]['id']);
					}
					
					$res=$good->update($update_arr,array('table'=>'goods_spec','id'=>$id_arr));
				}
				if($res)
		        {
			            $result['error']=0;
			             $result['content']=Lang('SUCCESS_EDIT');
		        }
		         else
		        {
			          $result['content']=Lang('INVALID_OPERATION');
		        }
		               $this->showMsg($result);
			}
			else 
			{
				$result['content']='标签更新失败';
				$this->showMsg($result);
			}
			
		
		
	//	$res=Model('goods')->update($info,array('table'=>'goods_attr','id'=>$id));
		if($res)
		{
			$result['error']=0;
			$result['content']=Lang('SUCCESS_EDIT');
		}
		else
		{
			$result['content']=Lang('INVALID_OPERATION');
		}
		$this->showMsg($result);
	}
	private function format_spec($name,$value)
	{
		$spec='';
		if($name)
		{
			foreach($name as $k=>$v)
			{
				if($value['val_'.$k])
				{
					$key=explode('_', $k);
					//$temp['name']=$v;
					$temp['spec_id']=$key[1];
					if(is_array($value['val_'.$k]))
					{
						$en_data=$value['val_'.$k][1];
						$temp['spec_value']=CH_SC.$value['val_'.$k][0].DIG_EC.EN_SC.$en_data.DIG_EC;
					}
					else
					{
						$temp['spec_value']=$value['val_'.$k];
					}
					$spec[]=$temp;
				}
			}
		}
		return $spec;
	}
	public function index()
	{
		$cat_id=intval($_GET['id']);
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		
		$art_list=Model('pro')->pro_list($page,$cat_id);
		
		
		$this->assign('pro_list',$art_list['data']);
		$this->assign('pro_page',$art_list['pagelink']);
		
		$this->assign('cat_id',$cat_id);
		$this->display('index.html');
		
	}
	public function del()
	{
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		if(is_array($id))
		{
			foreach($id as $k=>$v)
			{
				$result=Model('pro')->del_pro($v);
			}
		}
		else
		{
			$result=Model('pro')->del_pro($id);
		}
		print_r(json_encode($result));
	}
	public function edit_pro()
	{
	
		$id=isset($_GET['id'])?intval($_GET['id']):'';
		$this->assign('id',$id);
		$art_info=Model('pro')->get_pro($id);
		$this->assign('pro',$art_info);
		
		$img_arr=Model('pro')->get_pro_img($id);
		$this->assign('pro_img',$img_arr);
		$this->assign('action','modified_pro');
		$this->display('product.html');
	
	}
	public function modified_pro()
	{
		
		$id=isset($_POST['id'])?$_POST['id']:'';
		$info['title']=isset($_POST['title'])?$_POST['title']:'';
		$info['abstract']=isset($_POST['abstract'])?$_POST['abstract']:'';
		$info['keywords']=isset($_POST['keywords'])?$_POST['keywords']:'';
		$info['model']=isset($_POST['model'])?$_POST['model']:'';
		$info['size']=isset($_POST['size'])?$_POST['size']:'';
		$info['content']=isset($_POST['content'])?$_POST['content']:'';
		$info['hits']=isset($_POST['hits'])?$_POST['hits']:'';
		$info['is_show']=isset($_POST['is_show'])?$_POST['is_show']:'';
		$info['is_push']=isset($_POST['is_push'])?$_POST['is_push']:'';
		$img_array=isset($_POST['img_array'])?(array)json_decode($_POST['img_array']):'';
		$info['page_title']=isset($_POST['page_title'])?$_POST['page_title']:'';
		$info['created']=$_SERVER['REQUEST_TIME'];
		$info['page_title']=isset($_POST['page_title'])?$_POST['page_title']:'';
		$info['thumb']=isset($_POST['thumb'])?$_POST['thumb']:'';
		
		$result=Model('pro')->update_pro($info,$img_array,$id);
		
		
		$data=array('error'=>0,'content'=>'','url'=>'');
		if($result['error']==0)
		{
			$data['content']=Lang('SUCCESS_EDIT');
			$data['url']="index.php?g=admin&m=content&a=add_content&id=".$this->get_cat_id($id,'product');
			
		}
		else
		{
			$data['content']=Lang('INVALID_OPERATION');
			//$data['url']=$_SERVER['REQUEST_URI'];
		}
		$this->showMsg($data);
	}
     public function add_pro()
	{
		
		$cat_id=isset($_GET['cat_id'])?intval($_GET['cat_id']):'';
		$this->assign('cat_id',$cat_id);
		$this->assign('action','insert_pro');
		$this->display('product.html');
	
	}
	
	public function insert_pro()
	{
		$info['cat_id']=isset($_POST['cat_id'])?$_POST['cat_id']:'';
		$info['title']=isset($_POST['title'])?$_POST['title']:'';
		$info['abstract']=isset($_POST['abstract'])?$_POST['abstract']:'';
		$info['keywords']=isset($_POST['keywords'])?$_POST['keywords']:'';
		$info['model']=isset($_POST['model'])?$_POST['model']:'';
		$info['size']=isset($_POST['size'])?$_POST['size']:'';
		$info['content']=isset($_POST['content'])?$_POST['content']:'';
		$info['hits']=isset($_POST['hits'])?$_POST['hits']:'';
		$info['is_show']=isset($_POST['is_show'])?$_POST['is_show']:'';
		$info['is_push']=isset($_POST['is_push'])?$_POST['is_push']:'';
		$img_array=isset($_POST['img_array'])?(array)json_decode(stripslashes($_POST['img_array'])):'';
		$info['page_title']=isset($_POST['page_title'])?$_POST['page_title']:'';
		$info['created']=$_SERVER['REQUEST_TIME'];
		$info['page_title']=isset($_POST['page_title'])?$_POST['page_title']:'';
		$info['thumb']=isset($_POST['thumb'])?$_POST['thumb']:'';
		
		if($info['title']=='')
		{
			$data['content']=Lang('NAME_EMPTY');
			$this->showMsg($data);
		}
		$result=Model('pro')->create_pro($info,$img_array);
		$data=array('error'=>0,'content'=>'','url'=>'');
		if($result['error']==0)
		{
			$data['content']=Lang('SUCCESS_ADD');
			$data['url']="index.php?g=admin&m=content&a=add_content&id=".$info['cat_id'];
			
		}
		else
		{
			$data['content']=Lang('INVALID_OPERATION');
			//$data['url']=$_SERVER['REQUEST_URI'];
		}
		$this->showMsg($data);
	}
	
	
	
	

	
	
}